﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BISPOS.Repository;
using System.Linq;
namespace BISPOS
{
    public partial class EditCustomerUserControl : DevExpress.XtraEditors.XtraUserControl
    {
        CustomerRepository customerRepository = new CustomerRepository();
        List<Customer> allCustomers = new List<Customer>();
        public EditCustomerUserControl()
        {
            InitializeComponent();
            allCustomers = customerRepository.AllCustomers().ToList();
            this.searchTextEdit.Focus();
            
        }
        public void ChangeCustomer(Customer customer)
        {
            this.groupControl2.Controls.Clear();
            CustomerDetailsUserControl control = new CustomerDetailsUserControl(customer);
            this.groupControl2.Controls.Add(control);
            searchTextEdit.ResetText();
            searchTextEdit.Focus();

        }
        private void searchTextEdit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    Customer customer = customerRepository.GetCustomer(searchTextEdit.Text);
                    this.groupControl2.Controls.Clear();
                    if (customer != null)
                    {
                        CustomerDetailsUserControl productDetailsUserControl = new CustomerDetailsUserControl(customerRepository.GetCustomer(searchTextEdit.Text));


                        this.groupControl2.Controls.Add(productDetailsUserControl);
                    }
                    else { MessageBox.Show("ไม่พบสินค้า"); }
                    searchTextEdit.ResetText();

                }
                catch 
                { 
                    MessageBox.Show("ไม่พบลูกค้า");
                    searchTextEdit.ResetText();
                }
            }
            if (e.KeyCode == Keys.F1)
            {
                EditCustomer_SelectCustomerForm selectCustomer = new EditCustomer_SelectCustomerForm(this, allCustomers);
                selectCustomer.Show();
            }
        }

        private void searchTextEdit_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void EditCustomerUserControl_Load(object sender, EventArgs e)
        {
        }
    }
}
